package com.ruge.test.springMVC.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test;

/**
 * 创建人 ：爱丽丝、如歌
 * 创建时间 ：2018-07-02  22:05
 * 描述 ：
 */
public class demo02_01_授权测试 {


    SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();

    @Before
    public void addUser() {
        simpleAccountRealm.addAccount("root", "root", "admin");
    }

    @Test
    public void test1() {
        /**
         * 构建securityManager
         */
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        securityManager.setRealm(simpleAccountRealm);
        /**
         * 主体提交认证请求
         */
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("root", "root");
        subject.login(usernamePasswordToken);

        boolean authenticated = subject.isAuthenticated();
        System.out.println(authenticated);

        boolean admin = subject.hasRole("admin");
        System.out.println(admin);
        subject.checkRole("admin");
    }
}
